package com.ug.system.controller.tencent;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.net.URLEncodeUtil;
import com.ug.common.cache.redis.RedisCacheService;
import com.ug.common.response.ResponseMessage;
import com.ug.common.response.ResponseMessageFactory;
import com.ug.config.tencent.QQConfig;
import com.ug.system.entity.vo.QuictLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author LY
 * @create 2024/8/26 11:15
 * @Description 腾讯应用
 */
@Tag(name = "TencentController", description = "QQ快捷登录")
@RestController
@RequestMapping("/tencent")
@RequiredArgsConstructor
public class TencentController {

    private final QQConfig qqConfig;
    private final RedisCacheService redisCacheService;

    /**
     * 获取code
     */
    @Operation(summary = "QQ快捷登录获取code")
    @GetMapping("/getQQCode")
    public ResponseMessage<QuictLoginVO> getQQCode(){
        // 授权地址 ,进行Encode转码
        String authorizeURL = qqConfig.getAuthorizeUrl();
        // 回调地址 ,进行Encode转码
        String redirectUri = qqConfig.getRedirectUrl();
        // 用于第三方应用防止CSRF攻击
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 拼接参数
        String url = authorizeURL +
                "?response_type=code" +
                "&client_id=" + qqConfig.getAppId() +
                "&redirect_uri=" + URLEncodeUtil.encodeFragment(redirectUri) +
                "&state=" + uuid;
        redisCacheService.put(uuid, uuid, 60 * 5);
        QuictLoginVO vo = new QuictLoginVO();
        vo.setUuid(uuid);
        vo.setUrl(url);
        return ResponseMessageFactory.getSuccessResponseMessage(vo, null);
    }
}
